package com.dykj.fireFighting.web.base.app;

import cn.hutool.core.lang.Validator;
import com.dykj.fireFighting.api.base.bigdata.device.entity.Device;
import com.dykj.fireFighting.api.base.bigdata.device.service.IDeviceService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.base.news.service.IAliMessageService;
import com.dykj.fireFighting.api.base.rescue.entity.RescueInfo;
import com.dykj.fireFighting.api.base.rescue.service.IRescueInfoService;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;

/**
 * Created with IDEA
 *
 * @Author : HXB
 * @Date : 2021-09-09 15:45
 * @Description : 短信发送
 **/
@Slf4j
@Api(value = "app短信发送接口", tags = {"app短信发送接口"})
@RestController
@RequestMapping("/app/aliMessage")
public class AppAliMessage {


    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private IRescueInfoService rescueInfoService;
    @DubboReference
    private IDeviceService deviceService;
    @DubboReference
    private IAliMessageService aliMessageService;

    /**
     * 火情短信发送接口
     *
     * @param relationId
     * @return
     */
    @ApiOperation(value = "火情短信发送接口", notes = "火情短信发送接口")
    @GetMapping("/sendAliMessage")
    public CommonResult sendAliMessage(String relationId, String ztfssj) throws UnsupportedEncodingException {
        String enterpriseId = ShiroUtil.getUser().getEnterpriseId().toString();
        Enterprise enterprise = enterpriseService.getById(enterpriseId);
        RescueInfo rescueInfo = rescueInfoService.getById(enterprise.getResuseId());
        Device device = deviceService.getByRelationId(relationId);
        if (rescueInfo.getIsDel() == 1 && rescueInfo.getType() == 0) {
            String socialName = enterprise.getEnterpriseName();
            String socialAddress = enterprise.getAddress();
            String socialLongitude = enterprise.getLongitude();
            String socialLatitude = enterprise.getLatitude();
            String socialLocation = socialLatitude + "," + socialLongitude;

            String name = rescueInfo.getName();
            String tel = rescueInfo.getTel();
            String longitude = rescueInfo.getLongitude();
            String latitude = rescueInfo.getLatitude();
            String location = latitude + "," + longitude;

            String deviceName = device.getName();

            if (StringUtils.isBlank(tel)) {
                return CommonResult.failed("短信发送失败,手机号为空");
            } else if (!Validator.isMobile(tel)) {
                return CommonResult.failed("短信发送失败,手机号验证失败");
            }
            String code = aliMessageService.sendFireMessage(socialName, socialLocation, socialAddress, ztfssj, name, location, tel, deviceName);
            if ("OK".equals(code)) {
                return CommonResult.success("短信发送成功");
            }
            return CommonResult.failed("短信发送失败");
        }
        return CommonResult.failed("没有找到救援单位!");
    }

}
